using Microsoft.EntityFrameworkCore;
using YoYoMoocExampleApp.Models;

var builder = WebApplication.CreateBuilder(args);
var configBuilder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json").Build();

// Add services to the container.
// builder.Services.AddTransient<IProductRepository,MockProductRepository>();
builder.Services.AddTransient<IProductRepository, DataProductRepository>();
builder.Services.AddRazorPages();


var host = configBuilder["DBHOST"] ?? "172.19.0.2";
var port = configBuilder["DBPORT"] ?? "3306";
var password = configBuilder["DBPASSWORD"] ?? "bb123456";

var connectionStr = configBuilder["ConnectionString"] ?? $"server={host};userid=root;pwd={password};port={port};database=productsdb";


Console.WriteLine(connectionStr);

builder.Services.AddDbContextPool<ProductDbContext>(options => options.UseMySql(connectionStr, new MySqlServerVersion(new Version(8, 0, 25))));
// builder.Services.AddDbContextPool<ProductDbContext>(options => options.UseMySql(builder.Configuration["ConnectionStrings:MySql"],
//     new MySqlServerVersion(new Version(8, 0, 25))));

var app = builder.Build();



// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

    app.UseDataInitalizer();
// if ((configBuilder["INITDB"] ?? "false") == "true")
// {
// }

app.Run();
